<template>
  <div v-if="iWebPDFShow" class="container" v-html="dom"></div>
  <iframe width="100%" height="100%" v-else :src="fileurl"></iframe>
</template>

<script>
import dom from './iWebPDF2018'
import { BASE_URL } from '@/utils/request'

export default {
  data() {
    return {
      dom: dom,
      iWebPDF: null,
      iWebPDFShow: true,
      fileurl: '',
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.iWebPDF = iWebPDF2018.iWebPDFFun
      this.iWebPDF.WebUrl = BASE_URL + '/PDFServer.jsp'
      this.iWebPDF.ShowTools = 0 // 设置标准工具栏当前是否可见 0:隐藏   1:可见
      this.iWebPDF.ShowMenus = 0 // 设置是否显示菜单栏 0:隐藏   1:可见
      this.iWebPDF.ShowTabBarVisible = false // 是否显示窗口tab标签页。false：不显示窗口tab标签页：true：显示窗口tab标签页。
      this.iWebPDF.StartPageVisible = false // 窗口起始页是否显示
    },
    openFile(attachmentid, fileurl) {
      try {
        this.iWebPDF.WebClose()
        this.iWebPDF.RECORDID = attachmentid
        // this.iWebPDF.FILENAME = 'test.pdf'
        // this.iWebPDF.FILETYPE = '.pdf'
        // this.iWebPDF.USERNAME = '演示人'
        this.iWebPDF.WebOpen()
      } catch (error) {
        console.error('iWebPDF2018插件初始化失败，已启用附件查看模式', error)
        this.iWebPDFShow = false
        this.fileurl = fileurl
      }
      return this.iWebPDFShow
    },
    saveFile(attachmentid, filename, applyid, matter_attachid) {
      this.iWebPDF.RECORDID = attachmentid
      this.iWebPDF.FILENAME = filename
      this.iWebPDF.EXTPARAM = JSON.stringify({ applyid, matter_attachid })

      // 检测文档是否修改。0为文档未修改，非零为文档已修改
      if (this.iWebPDF.DocumentModified != 0) {
        // 保存失败为false
        if (!this.iWebPDF.WebSave()) {
          this.iWebPDF.Alert('批注附件保存失败')
        }
      }
    },
    bringBackAnnotate() {
      // 获取批注信息json格式，有问题不使用
      // let annotString = this.iWebPDF.GetAnnotString()
      // 获取批注信息xml格式
      let annotXml = this.iWebPDF.GetTextPostilInfo()
      let domParser = new DOMParser()
      let xmlDoc = domParser.parseFromString('<Comments>' + annotXml + '</Comments>', 'text/xml')
      let contents = xmlDoc.getElementsByTagName('Contents')
      let texts = []
      if (contents && contents.length) {
        for (let i = 0; i < contents.length; i++) {
          texts.push(contents[i].textContent)
        }
      }
      return texts
    }
  }
}
</script>

<style scoped>
.container {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
